package com.lyn.solitude_plugin_host_ui.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;
import com.lyn.solitude_plugin_host_ui.databinding.AdapterPluginEmptyItemBinding;
import com.lyn.solitude_plugin_host_ui.databinding.AdapterPluginItemBinding;
import com.lyn.solitude_plugin_api.entity.Plugin;
import java.util.ArrayList;
import java.util.List;

/**
 * 插件适配器
 *
 * @author longyn 2024/4/20
 * @version 1.0.0
 */
public class PluginAdapter extends RecyclerView.Adapter<PluginAdapter.PluginViewHolder> {

    private final String TAG = getClass().getSimpleName();
    private EmptyPluginListener emptyPluginListener;
    private final List<Plugin.Simple> list = new ArrayList<>();
    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;

    @SuppressLint("NotifyDataSetChanged")
    public void setPluginData(List<Plugin.Simple> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public void setEmptyPluginListener(@NonNull EmptyPluginListener emptyPluginListener) {
        this.emptyPluginListener = emptyPluginListener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void clear() {
        this.list.clear();
        notifyDataSetChanged();
    }

    public Plugin.Simple getItem(int position) {
        if (list.isEmpty()) {
            return null;
        }
        return list.get(position);
    }

    @Override
    public int getItemViewType(int position) {
        if (this.list.isEmpty()) {
            return -1;
        }
        return super.getItemViewType(position);
    }

    @NonNull
    @Override
    public PluginViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == -1) {
            AdapterPluginEmptyItemBinding binding = AdapterPluginEmptyItemBinding.inflate(
                    LayoutInflater.from(parent.getContext()), parent, false);
            ViewGroup.LayoutParams layoutParams = binding.getRoot().getLayoutParams();
            layoutParams.width = -1;
            layoutParams.height = -1;
            return new PluginViewHolder(binding);
        }
        AdapterPluginItemBinding binding = AdapterPluginItemBinding.inflate(
                LayoutInflater.from(parent.getContext()), parent, false);
        ViewGroup.LayoutParams layoutParams = binding.getRoot().getLayoutParams();
        layoutParams.width = parent.getWidth() / 2;
        layoutParams.height = parent.getWidth() / 2;
        return new PluginViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull PluginViewHolder holder, int position) {
        if (this.list.isEmpty()) {
            return;
        }
        Plugin.Simple simple = this.list.get(position);
        if (simple == null) {
            return;
        }
        if (holder.binding instanceof AdapterPluginItemBinding) {
            AdapterPluginItemBinding binding = (AdapterPluginItemBinding) holder.binding;
            binding.name.setText(simple.getName());
            holder.binding.getRoot().setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(holder, position);
                }
            });
            holder.binding.getRoot().setOnLongClickListener(v -> {
                if (onItemLongClickListener != null) {
                    return onItemLongClickListener.onItemLongClick(holder, position);
                }
                return false;
            });
        }
    }

    @Override
    public int getItemCount() {
        if (this.list.isEmpty()) {
            if (emptyPluginListener != null) {
                emptyPluginListener.onEmpty();
            }
            return 1;
        }
        if (emptyPluginListener != null) {
            emptyPluginListener.onNotEmpty();
        }
        return this.list.size();
    }

    public static class PluginViewHolder extends RecyclerView.ViewHolder {

        public ViewBinding binding;

        public PluginViewHolder(@NonNull ViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    public interface EmptyPluginListener {
        void onEmpty();

        void onNotEmpty();
    }

    public interface OnItemClickListener {
        void onItemClick(PluginViewHolder holder, int position);
    }

    public interface OnItemLongClickListener {
        boolean onItemLongClick(PluginViewHolder holder, int position);
    }

}
